package com.millstein.realtime.app.dwd.db;

import com.millstein.realtime.app.base.BaseSqlApp;
import com.millstein.realtime.common.Constants;
import com.millstein.realtime.util.SqlUtil;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

/**
 * @Description
 * @Author tsing
 * @Date 2024-10-12 8:35
 */
public class Dwd_09_TradeOrderRefund extends BaseSqlApp {

    public static void main(String[] args) {
        new Dwd_09_TradeOrderRefund().init(
                7003,
                2,
                "Dwd_09_TradeOrderRefund"
        );
    }

    /**
     * 具体数据处理的逻辑，由子类编写
     *
     * @param env      执行环境
     * @param tableEnv 表执行环境
     */
    @Override
    public void handle(StreamExecutionEnvironment env, StreamTableEnvironment tableEnv) {
        // 1.设置数据过期时间
        tableEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(5));

        // 2.读取ods层的数据
        readOdsDataFromKafka(tableEnv, "Dwd_09_TradeOrderRefund");

        // 3.筛选出退单的数据
        Table orderRefundInfo = tableEnv.sqlQuery(
                "select " +
                "    `data`['id'] id, " +
                "    `data`['user_id'] user_id, " +
                "    `data`['order_id'] order_id, " +
                "    `data`['sku_id'] sku_id, " +
                "    `data`['refund_type'] refund_type, " +
                "    `data`['refund_num'] refund_num, " +
                "    `data`['refund_amount'] refund_amount, " +
                "    `data`['refund_reason_type'] refund_reason_type, " +
                "    `data`['refund_reason_txt'] refund_reason_txt, " +
                "    `data`['create_time'] create_time, " +
                "    `pt`, " +
                "    `ts` " +
                "from maxwell_table " +
                "where `database` = 'gmall' " +
                "    and `table` = 'order_refund_info' " +
                "    and `type` = 'insert'"
        );
        tableEnv.createTemporaryView("order_refund_info", orderRefundInfo);

        // 4.从ods层数据中筛选出订单数据。因为退单数据中没有省份信息，后续需要省份信息来进行分析
        Table orderInfo = tableEnv.sqlQuery(
                "select " +
                "    `data`['id'] id, " +
                "    `data`['province_id'] province_id " +
                "from maxwell_table " +
                "where `type` = 'update' " +
                "    and `database` = 'gmall' " +
                "    and `table` = 'order_info' " +
                "    and `old`['order_status'] is not null " +
                "    and `data`['order_status'] = '1005'"
        );
        tableEnv.createTemporaryView("order_info", orderInfo);

        // 5.读取base_dic中的数据
        readBaseDicFromMysql(tableEnv);

        // 6.三张表进行join（base_dic要join两次，因为要退化refund_type和refund_reason_type两个字段）
        Table resultTable = tableEnv.sqlQuery(
                "select " +
                "    ori.id, " +
                "    ori.user_id, " +
                "    oi.province_id, " +
                "    ori.order_id, " +
                "    ori.sku_id, " +
                "    ori.refund_type refund_type_code, " +
                "    bd1.dic_name refund_type_name, " +
                "    ori.refund_num, " +
                "    ori.refund_amount, " +
                "    ori.refund_reason_type refund_reason_type_code, " +
                "    bd2.dic_name refund_reason_type_name, " +
                "    ori.refund_reason_txt, " +
                "    date_format(ori.create_time, 'yyyy-MM-dd') date_id, " +
                "    ori.ts, " +
                "    current_row_timestamp() row_opt_ts " +
                "from order_info oi " +
                "join order_refund_info ori on oi.id = ori.order_id " +
                "join base_dic for system_time as of ori.pt as bd1 on bd1.dic_code = ori.refund_type " +
                "join base_dic for system_time as of ori.pt as bd2 on bd2.dic_code = ori.refund_reason_type"
        );

        // 7.创建kafka-sink的动态表
        tableEnv.executeSql(
                "create table dwd_trade_order_refund ( " +
                "    id string, " +
                "    user_id string, " +
                "    province_id string, " +
                "    order_id string, " +
                "    sku_id string, " +
                "    refund_type_code string, " +
                "    refund_type_name string, " +
                "    refund_num string, " +
                "    refund_amount string, " +
                "    refund_reason_type_code string, " +
                "    refund_reason_type_name string, " +
                "    refund_reason_txt string, " +
                "    date_id string, " +
                "    ts bigint, " +
                "    row_opt_ts timestamp_ltz(3) " +
                ")" + SqlUtil.getKafkaSinkDDL(Constants.TOPIC_DWD_TRADE_ORDER_REFUND)
        );

        // 8.将结果数据写入kafka
        resultTable.executeInsert("dwd_trade_order_refund");
    }
}
